<?php
/**
 * Created by PhpStorm.
 * User: 86188
 * Date: 2021/1/28
 * Time: 17:53
 */

namespace app\wxpro\controller;

use app\service\wotuService;
use think\facade\Db;
use think\Log;


class Device
{
    public function doorlist(){
        $ctype=request()->param("ctype");
        $openid=request()->param("openid");
        $wxuserinfo = Db::table("wxuser")->where("openid",$openid)->find();
        $doorlist = Db::table("wxuser_device")->where("wxuser_id",$wxuserinfo["id"])->select();
        $list=[];
        foreach ($doorlist as $val){
            $deviceinfo = Db::table("device")->where("id",$val["deviceid"])->find();
            $val["devicename"] =$deviceinfo["devicename"];
            $list[] = $val;
        }


        return json_encode(["errCode"=>"0","doorList"=>$list]);

    }

    public function deviceopen(){
        $openid=request()->post("openid");
        $deviceid=request()->post("deviceid");
        $deviceinfo=Db::table("device")->where("id",$deviceid)->find();
        $wxuserinfo=Db::table("wxuser")->where("openid",$openid)->find();
        if($wxuserinfo["community_id"]!=$deviceinfo["community_id"]){
            return json_encode(["errCode"=>"2","errormsg"=>"无权操作"]);
        }
        $res=(new wotuService())->devopen($deviceinfo["devicekey"]);
        Log::write($res,"远程开门");
        if($res["code"]=="WO_SUS1000"){
            if($deviceinfo["devicetype"]==3){
                if(($deviceinfo["wiegandStatus"]=="1")&&(!empty($deviceinfo["localurl"]))){
                    $url = "http://".$deviceinfo["localurl"].":8090/setWeigenContent";
                    $recSucWiegandContent = $wxuserinfo["wiegand"];
                    $this->curl_post($url,$recSucWiegandContent);
                }
            }
            return json_encode(["errCode"=>"0","msg"=>"开门成功"]);
        }else{
            return json_encode(["errCode"=>"2","errormsg"=>"开门失败"]);
        }
    }
    public function open(){
        $userid=request()->param("user_id");
        $scanType=request()->param("scantype");
        if($scanType!="applet"){
            echo "<script>window.location.href=\"alipays://platformapi/startapp?appId=2021002124609150&page=pages/index/index\"</script>";
            echo "小程序";die;
        }
        if(strlen($userid)!="16"){
            return json_encode(["errcode"=>"1","errmsg"=>"开门失败"]);
        }
        //查询用户是否绑定

        $userinfo=Db::table("personnel")->where("user_id",$userid)->find();
        if(empty($userinfo)){
            return json_encode(["errcode"=>"2","errmsg"=>"亲业的业主！请验证成功后开启开门权限"]);
        }

        $lockid=request()->param("id");

        $deviceinfo=Db::table("device")->where("id",$lockid)->find();
        if($userinfo["community_id"]!=$deviceinfo["community_id"]){
            return json_encode(["errcode"=>"2","errmsg"=>"无权操作"]);
        }

        $res=(new wotuService())->devopen($deviceinfo["devicekey"]);
        $res=json_decode($res,true);
        if($res["code"]=="WO_SUS1000"){
            $info["relation"]=$userinfo["relation"];
            $info["lockid"]=$deviceinfo["id"];
            $info["personnel_id"]=$userinfo["id"];
            $info["create_time"]=date("Y-m-d H:i:s");
            $info["userid"]=$userid;
            Db::table("record_scan")->insert($info);
            return json_encode(["errcode"=>"0","msg"=>"开门成功"]);
        }else{
            return json_encode(["errcode"=>"2","errmsg"=>"开门成功"]);
        }
    }

    public function curl_post($url,$recSucWiegandContent){


        $pass = "202188";
        $recSucWiegandType="3";

        $data = ['pass'=>$pass,'recSucWiegandType'=>$recSucWiegandType,'recSucWiegandContent'=>$recSucWiegandContent];
        $headers =array('Content-Type: application/x-www-form-urlencoded');
        $curl = curl_init();// 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL,$url);// 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);// 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);// 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);// 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);// 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);// 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1);// 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));// Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);// 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0);// 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);// 获取的信息以文件流的形式返回
        curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
        $result = curl_exec($curl);// 执行操作
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl);// 关闭CURL会话
        return $result;
    }
}